package com.tensquare.user.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.tensquare.user.pojo.User;
import com.tensquare.user.service.UserService;

import entity.PageResult;
import entity.Result;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * 控制器层
 * @author Administrator
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private JwtUtil jwtUtil;
	/**
	 * 查询全部数据
	 * @return
	 */
	@RequestMapping(value="/",method=RequestMethod.GET)
	public List<User> findAll(){
		return userService.findAll();
	}
	
	/**
	 * 根据ID查询
	 * @param id ID
	 * @return
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public User findOne(@PathVariable String id){
		return userService.findOne(id);
	}
	
	/**
	 * 分页查询全部数据
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/{page}/{size}",method=RequestMethod.GET)
	public PageResult<User> findPage(@PathVariable int page,@PathVariable int size){		
		Page<User> pageList = userService.findPage(page, size);		
		return new PageResult<User>(pageList.getTotalElements(), pageList.getContent());		
	}
	
	/**
	 * 分页+多条件查询 
	 * @param searchMap 查询条件封装
	 * @param page 页码
	 * @param size 页大小
	 * @return 分页结果
	 */
	@RequestMapping(value="/{page}/{size}",method=RequestMethod.POST)
	public PageResult<User> findSearch(@RequestBody Map searchMap ,@PathVariable int page,@PathVariable int size){		
		Page<User> pageList = userService.findSearch(searchMap, page, size);
		return new PageResult<User>(pageList.getTotalElements(), pageList.getContent());		
	}
	
	/**
	 * 增加
	 * @param user
	 */
	@RequestMapping(value="/register/{code}",method=RequestMethod.POST)
	public Result add(@RequestBody User user,String code  ){

		userService.add(user,code);
		
		return new Result(true,0,"");
	}
	
	/**
	 * 修改
	 * @param user
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.PUT)
	public Result update(@RequestBody User user,@PathVariable String id ){
		user.setId(id);
		userService.update(user);		
		return new Result(true,0,"");
	}
	
	/**
	 * 删除
	 * @param id
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.DELETE)
	public Result delete(@PathVariable String id ){
		Claims claims=(Claims) request.getAttribute("admin_claims");
		if(claims==null){
			return new Result(true,StatusCode.ACCESSERROR,"无权访问");
		}
		userService.delete(id);
		return new Result(true,0,"");
	}
	/**
	 * 发送短信验证码
	 * @param mobile
	 */
		@RequestMapping(value="/sendsms/{mobile}",method=RequestMethod.POST)
	public Result sendsms(@PathVariable String mobile ){
		userService.sendSms(mobile);
		return new Result(true,StatusCode.OK,"发送成功");
	}
	/**
	 * 用户登陆
	 * @param
	 * @param
	 * @return
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public Result login(@RequestBody Map<String,String> loginMap){
		User user = userService.findByMobileAndPassword(loginMap.get("mobile"),loginMap.get("password"));
		if(user!=null){
			String token = jwtUtil.createJWT(user.getId(),
					user.getNickname(), "user");
			Map map=new HashMap();
			map.put("token","Bearer "+token);
			map.put("name",user.getNickname());
			map.put("avatar",user.getAvatar());
			map.put("id",user.getId());
			return new Result(true,StatusCode.OK,"登陆成功",map);
		}else{
			return new Result(false,StatusCode.LOGINERROR,"用户名或密码错误");
		}
	}
	/**
	 * 增加粉丝数
	 * @param userid
	 * @param x
	 */
	@RequestMapping(value="/incfans/{userid}/{x}",method=RequestMethod.POST)
	public Result incFanscount(@PathVariable String userid,@PathVariable int x){
		userService.incFanscount(userid,x);
		return new Result(true,StatusCode.ACCESSERROR,"粉丝增加");
	}
	/**
	 * 增加关注数
	 * @param userid
	 * @param x
	 */
	@RequestMapping(value="/incfollow/{userid}/{x}",method=RequestMethod.POST)
	public void incFollowcount(@PathVariable String userid,@PathVariable int x){
		userService.incFollowcount(userid,x);
	}

	@RequestMapping(value = "/yuehui",method = RequestMethod.POST)
	public Result yuehui(@RequestBody User user){
		System.out.println(user.getId());
		User user1 = userService.findOne(user.getId());
		user1.setInterest(user.getInterest());
		userService.update(user1);
		return new Result(true,StatusCode.OK,"操作成功");
	}
}
